import { defineConfig,loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from "path";
import Unocss from "unocss/vite";
import { presetAttributify, presetUno, transformerDirectives } from "unocss";
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

// https://vitejs.dev/config/
export default defineConfig(({mode}) => {
    const env = loadEnv(mode,process.cwd())

    return {
        resolve: {
            alias: {
                "@": resolve(__dirname, "./src"),
            },
        },
        plugins: [
            vue(),
            AutoImport({
                resolvers: [ElementPlusResolver()],
                include: [
                    /\.[tj]sx?$/, // .ts, .tsx, .js, .jsx
                    /\.vue$/, /\.vue\?vue/, // .vue
                ],
                imports: [
                    'vue',
                    'pinia'
                ],
                dirs: [
                    './src/utils',
                    './src/api'
                ],
                dts: 'src/auto-import.ts',
                vueTemplate: true,
            }),
            Components({
                resolvers: [ElementPlusResolver()],
            }),
            Unocss({
                presets: [presetAttributify({}), presetUno()],
                rules: [
                    [/^fs-(\d+\.{0,1}\d{0,2})$/, ([, d]) => ({ "font-size": `${d}px` })],
                    [/^leh-(\d+\.{0,1}\d{0,2})$/, ([, d]) => ({ "line-height": `${d}px` })],
                    [/^mt(\d+\.{0,1}\d{0,2})$/, ([, d]) => ({ "margin-top": `${d}px` })],
                    [/^mr(\d+\.{0,1}\d{0,2})$/, ([, d]) => ({ "margin-right": `${d}px` })],
                    [/^ml(\d+\.{0,1}\d{0,2})$/, ([, d]) => ({ "margin-left": `${d}px` })],
                    [/^mb(\d+\.{0,1}\d{0,2})$/, ([, d]) => ({ "margin-bottom": `${d}px` })],
                    [/^fw-(\d+\.{0,1}\d{0,2})$/, ([, d]) => ({ "font-weight": `${d}` })],
                    [/^pr(\d+\.{0,1}\d{0,2})$/, ([, d]) => ({ "padding-right": `${d}px` })],
                    [/^pl(\d+\.{0,1}\d{0,2})$/, ([, d]) => ({ "padding-left": `${d}px` })],
                ],
                transformers: [transformerDirectives()],
            }),
        ],
        css: {
            preprocessorOptions: {
                scss: {
                    //引用scss变量文件
                    additionalData: `@import "@/styles/variables.scss";@import "@/styles/mixin.scss";`,
                }
            }
        },
        server: {
            proxy: {
                '/api': {
                    target: 'http://api.codelife.cc',
                    rewrite: path => path.replace(/^\/api/, '')
                }
            }
        }
    }
})
